package cn.xbtop.blog.controller;

import cn.xbtop.blog.pojo.Music;
import cn.xbtop.blog.service.MusicService;
import cn.xbtop.blog.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/4/17 16:37
 */
@RestController
@RequestMapping("/music")
@Api(description = "音乐管理", value = "MusicController")
public class MusicController {

    @Autowired
    private MusicService musicService;

    /**
     * 添加音乐
     *
     * @param music
     * @return
     */
    @ApiOperation(value = "添加音乐")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<Object> save(@RequestBody Music music) {
        musicService.insert(music);

        return new Result<>("添加音乐成功！");
    }

    /**
     * 根据id修改音乐信息
     *
     * @param music
     * @return
     */
    @ApiOperation(value = "根据id修改音乐信息")

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result<Object> update(@RequestBody Music music) {
        musicService.updateByPrimaryKey(music);

        return new Result<>("修改音乐成功！");
    }

    /**
     * 查询所有音乐
     *
     * @return
     */
    @ApiOperation(value = "查询所有音乐")

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public Result<Object> findAll() {
        List<Music> list = musicService.findAll();
        return new Result<>(list);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询")

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Result<Object> findById(@PathVariable Integer id) {
        Music music = musicService.selectByPrimaryKey(id);
        return new Result<>(music);
    }

    /**
     * 根据id删除音乐
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除音乐")

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result<Object> delete(@PathVariable Integer id) {
        musicService.deleteByPrimaryKey(id);
        return new Result<>("删除音乐成功！");
    }

    /**
     * 启用
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "启用")

    @RequestMapping(value = "/enable/{id}", method = RequestMethod.PUT)
    public Result<Object> enable(@PathVariable Integer id) {
        musicService.enable(id);
        return new Result<>("启用音乐成功！");
    }

    /**
     * 弃用
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "弃用")
    @RequestMapping(value = "/disenable/{id}", method = RequestMethod.PUT)
    public Result<Object> disEnable(@PathVariable Integer id) {
        musicService.disEnable(id);
        return new Result<>("弃用音乐成功！");
    }
}
